package com.example.cslp.controller;

import com.example.cslp.pojo.entity.Course;
import com.example.cslp.pojo.entity.User;
import com.example.cslp.service.UserService;
import com.example.cslp.util.TimeUtil;
import com.example.cslp.web.JsonResult;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    //解决报错注解
    @ResponseBody
    @CrossOrigin

    //登录登录登录登录
    @PostMapping("/login")
    public ResponseEntity<Boolean> loginIn(@RequestBody User user) throws NoSuchAlgorithmException {
        System.out.println(user.getPassword());
        user.setPassword(getSHA(user.getPassword()));//密码加密
        System.out.println(user.getPassword());
        return userService.login(user);
    }
    //  签到加积分,user只有一个name参数
    @PostMapping("/signIn")
    public JsonResult point(@RequestBody User user){
        String today=getCurrentTime();
        User user1=userService.getUserInfo(user.getName());
        System.out.println(today+"*****************************");
        System.out.println(user1.getLastdate());
        //空的
        if(user1.getLastdate()==null){
            //修改数据库签到时间
            user1.setLastdate(today);
            //point签到，并直接修改最后签到时间
            return  JsonResult.ok( userService.point(user1),"签到成功");
        }
        else if(!(user1.getLastdate().equals(today))){//不是空的且不相等
            user1.setLastdate(today);//给user今天的时间
            return  JsonResult.ok( userService.point(user1),"签到成功");

        }
        else{//不空也相等，签到失败
            return JsonResult.ok(false,"今天已经签到过了");
        }
    }
    //注册
    @CrossOrigin
    @RequestMapping("/register")
    public ResponseEntity<Boolean> register(@RequestBody User user) throws NoSuchAlgorithmException {
        System.out.println(user.getPassword());
        user.setPassword(getSHA(user.getPassword()));//密码加密
        System.out.println(user.getPassword());
        //注入注册时间
        TimeUtil timeUtil=new TimeUtil();
        user.setRegisterTime(timeUtil.getCurrentTimePlus());
        //默认昵称、默认邮箱、默认手机号
        user.setNickname("default_nickname_"+user.getName());
        user.setMail("default_mail_"+user.getName());
        user.setPhone("default_phone"+ user.getName());
        return userService.register(user);
    }

    //主页面的用户信息
    @CrossOrigin
    @GetMapping("/getUserInfo")
//    public ResponseEntity<User> getUserInfo(@RequestParam String name){
//        //没有查密码
//        return ResponseEntity.ok(userService.getUserInfo(name));
//    }
    public ResponseEntity<User> getUserInfo(@RequestParam String name){
        System.out.println(name);
        User user = userService.getUserInfo(name);
        if (user != null) {
            // 不返回密码字段
            user.setPassword(null);
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.status(404).body(null);
        }
    }

    //编辑用户信息
//    @RequestMapping("/editUser")
//    public ResponseEntity<Boolean> editUser(@RequestBody User user){
//        return ResponseEntity.ok(userService.editUser(user));
//    }
    @CrossOrigin
    @PutMapping("/editInfo")
    public ResponseEntity<Object> editInfo(@RequestBody User user, @RequestHeader ("check_name") String check_name){
        System.out.println(check_name);
        return ResponseEntity.ok(userService.editInfo(user,check_name));
    }
    //编辑账户使用状态
    @RequestMapping("/editStatus")
    public ResponseEntity<Boolean> editStatus(@RequestBody User user){
        return ResponseEntity.ok(userService.editStatus(user));
    }
    //修改用户密码
//    @RequestMapping("/editPassword")
//    public ResponseEntity<Boolean> editPassword(@RequestBody User user) throws NoSuchAlgorithmException {
//        user.setPassword(getSHA(user.getPassword()));//密码加密
//        return ResponseEntity.ok(userService.editPassword(user));
//    }
    @CrossOrigin
    @PutMapping("/editPassword")
    public ResponseEntity<Boolean> editUser(@RequestParam("newPassword") String newPassword, @RequestParam("id") String id) throws NoSuchAlgorithmException {
        String new_password = getSHA(newPassword); // 密码加密
        return ResponseEntity.ok(userService.editPassword(new_password, id));
    }

    //    //根据用户获取选课信息
//    @RequestMapping("/getRecordByUser")
//    public ResponseEntity<List<Course>> getRecordByUser(@RequestBody User user)
//    {
//        return userService.getRecordByUser(user);
//    }
    //哈希密码加密
    public static String getSHA(String str) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] shaBytes = md.digest(str.getBytes());
        return bytesToHexString(shaBytes);
    }

    //将shaBytes转化成String(16进制）
    public static String bytesToHexString(byte[] bytes) {
        StringBuilder stringBuilder = new StringBuilder();
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        for (byte aByte : bytes) {
            int v = aByte & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }


    //返回当前系统时间戳
    public  Timestamp getDateTime(){
        Timestamp d = new Timestamp(System.currentTimeMillis());
        return d;
    }
    //返回格式化时间
    public  String getCurrentTime(){
        // 创建一个SimpleDateFormat对象，用于格式化时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 获取系统当前时间
        java.util.Date date=new java.util.Date();

        // 使用SimpleDateFormat对象将时间格式化为指定格式
        String currentTime = sdf.format(date);

        return currentTime;

    }
}
